﻿using System;
using System.Runtime.Serialization;

using Game.Common;
using Game.Common.Resources;
using Game.ExtensibleObjects;
using Game.Map;
using Game.ResourceLocator;
using Game.Types;

namespace Game.Objects
{
    [DataContract(Namespace="game")]
    public class GameBuilding : 
        IRuntimeExtensibleObject, 
        IGlobalItem, 
        IHasUrl,
        IHasOwner
    {
        public event EventHandler<HasOwnerEventArgs> PlayerChanged;

        GamePlayer _Owner;
        RuntimeExtensionCollection _Extensions;
        GameResources _Tank;
        
        [DataMember(Name="Url")]
        public string _Url;
        
        [DataMember(Name="BuildingType")]
        public readonly BuildingType _BuildingType;
        
        [DataMember(Name="Owner")]
        public string _OwnerUrl;

        public GameBuilding(BuildingType buildingType)
        {
            _BuildingType = buildingType;
            _Owner = HumanPlayer.Player;
            _OwnerUrl = _Owner.Url;
            _Url = Guid.NewGuid().ToString();
            _Tank = new GameResources(buildingType.Tank);

            _Extensions = buildingType.RuntimeExtensionFactories.GetExtensionCollection(this);
        }

        [OnDeserialized]
        public void OnDeserialized(StreamingContext c)
        {
            if (_OwnerUrl != null)
            {
                object obj = GameResourceLocator.Get(_OwnerUrl);
                var t = obj as GamePlayer;
                if (t != null)
                {
                    _Owner = t;
                }
            }

            var buildingType = GameResourceLocator.Get(_BuildingType.Url) as BuildingType;
            if (buildingType != null)
            {
                _Extensions = buildingType.RuntimeExtensionFactories.GetExtensionCollection(this);
                _Tank = new GameResources(buildingType.Tank);
            }

            GameResourceLocator.Register(this);
        }

        public RuntimeExtensionCollection Extensions 
        {
            get { return _Extensions; }
        }

        public string Url 
        {
            get { return _Url; }
        }
        
        public BuildingType BuildingType
        {
            get { return _BuildingType; }
        }
        
        public GamePlayer Owner
        {
            get { return _Owner; }
            set 
            { 
                _Owner = value; 
                _OwnerUrl = _Owner.Url;
                FirePlayerChanged();
            }
        }

        public GameResources Tank
        {
            get { return _Tank; }
        }

        public override string ToString()
        {
            string s = "{Building-Player";
            if (Owner != null)
            {
                s += Owner;
            }
            s += "}";
            return s;
        }
        
        void FirePlayerChanged()
        {
            if (PlayerChanged != null)
            {
                var args = new HasOwnerEventArgs(this);
                PlayerChanged(this, args);
            }
        }
    }
}
